package com.ygqh.baby.controller.mobile;

import com.fasterxml.jackson.databind.util.JSONPObject;
import com.ygqh.baby.ao.Message;
import com.ygqh.baby.ao.SalesPlatform;
import com.ygqh.baby.model.BaseSkuInfo;
import com.ygqh.baby.model.CommitOrderRequestParam;
import com.ygqh.baby.model.ProductBookRecordModel;
import com.ygqh.baby.model.YgShoppingCarModel;
import com.ygqh.baby.po.YgProductBook;
import com.ygqh.baby.po.YgUser;
import com.ygqh.baby.service.YgPrepayBookRecordService;
import com.ygqh.baby.utils.SessionUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicReference;

/**
 * @Auther: sunshuo
 * @Date: 2018/7/30 11:22
 * @Description:
 */
@Controller
@RequestMapping(value = "mobile/productbook")
public class YgProductBookController {

	private final SalesPlatform salesPlatform = SalesPlatform.Yiigoo;

	@Autowired
	private YgPrepayBookRecordService ygPrepayBookRecordService;

	/**
	 * 结算获取商品信息
	 *
	 * @param skuId
	 * @param quantity
	 * @param callback
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "findItemInfo", method = RequestMethod.GET)
	@ResponseBody
	public JSONPObject findItemInfo(Long skuId, Long quantity, String callback) {
		YgUser user = SessionUtil.getCurrentUser();
		Message validateMsg = ygPrepayBookRecordService.validateProductWithProductBook(Objects.requireNonNull(user).getId(), new BaseSkuInfo(skuId, quantity));
		if (!Message.isSuccess(validateMsg)) {
			return new JSONPObject(callback, validateMsg);
		}
		Map<String, Object> data = (Map<String, Object>) validateMsg.getExtra();
		List<YgShoppingCarModel> carList = (List<YgShoppingCarModel>) data.get("carList");
		YgProductBook productBook = (YgProductBook) data.get("productBook");
		Map<String, Object> extra = ygPrepayBookRecordService.getProductBookPrice(carList.get(0), productBook);
		extra.put("carList", carList);
		return new JSONPObject(callback, Message.success(extra));
	}

	/**
	 * 提交定金预售记录
	 *
	 * @param skuId
	 * @param quantity
	 * @param totalPrice 支付金额
	 * @param callback
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "commitRecord")
	@ResponseBody
	public JSONPObject commitRecord(@RequestParam Long skuId, @RequestParam Long quantity, @RequestParam BigDecimal totalPrice, String callback) {
		YgUser user = SessionUtil.getCurrentUser();
		Message validateMsg = ygPrepayBookRecordService.validateProductWithProductBook(Objects.requireNonNull(user).getId(), new BaseSkuInfo(skuId, quantity));
		if (!Message.isSuccess(validateMsg)) {
			return new JSONPObject(callback, validateMsg);
		}
		Map<String, Object> data = (Map<String, Object>) validateMsg.getExtra();
		List<YgShoppingCarModel> carList = (List<YgShoppingCarModel>) data.get("carList");
		YgProductBook productBook = (YgProductBook) data.get("productBook");
		if (productBook.getBookMoney().compareTo(totalPrice) != 0) {
			return new JSONPObject(callback, Message.error("提交失败，支付金额和定金金额不符", null));
		}
		return new JSONPObject(callback, ygPrepayBookRecordService.commitRecord(user.getId(), carList.get(0), productBook));
	}

	/**
	 * 取消活动订单
	 * @param recordCode
	 * @param callback
	 * @return
	 */
	@RequestMapping(value = "cancelRecord")
	@ResponseBody
	public JSONPObject cancelRecord(@RequestParam Long recordCode, String callback) {
		return new JSONPObject(callback, ygPrepayBookRecordService.cancelRecord(recordCode));
	}

	/**
	 * 活动订单退定金
	 * @param recordCode
	 * @param callback
	 * @return
	 */
	@RequestMapping(value = "refundRecord")
	@ResponseBody
	public JSONPObject refundRecord(@RequestParam Long recordCode, String callback) {
		/*return new JSONPObject(callback, ygPrepayBookRecordService.refundRecord(recordCode));*/
		return new JSONPObject(callback, Message.error("活动订单不支持退定金", null));
	}

	/**
	 * 活动订单合单结算-商品信息
	 *
	 * @param recordCode
	 * @param callback
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "findItemInfoByBookRecord", method = RequestMethod.GET)
	@ResponseBody
	public JSONPObject findItemInfoByBookRecord(@RequestParam String recordCode, String callback) {
		Message validateRecordCodeMessage = ygPrepayBookRecordService.validateRecordCode(recordCode);
		if (!Message.isSuccess(validateRecordCodeMessage)) {
			return new JSONPObject(callback, validateRecordCodeMessage);
		}
		List<ProductBookRecordModel> recordList = (List<ProductBookRecordModel>) validateRecordCodeMessage.getExtra();
		return new JSONPObject(callback, ygPrepayBookRecordService.processItemInfoWithRecordList(recordList, Objects.requireNonNull(SessionUtil.getCurrentUser()).getId(), null));
	}

	/**
	 * 提交订单（定金记录生成订单）
	 *
	 * @param requestParam 请求参数
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "commitOrder")
	@ResponseBody
	public JSONPObject commitOrder(CommitOrderRequestParam requestParam) {
		if (StringUtils.isBlank(requestParam.getSourceCode()) || requestParam.getSourceCode().equals("undefined")) {
			requestParam.setSourceCode(null);
		}
		Message validateRecordCodeMessage = ygPrepayBookRecordService.validateRecordCode(requestParam.getRecordCode());
		if (!Message.isSuccess(validateRecordCodeMessage)) {
			return new JSONPObject(requestParam.getCallback(), validateRecordCodeMessage);
		}
		AtomicReference<List<ProductBookRecordModel>> recordList = new AtomicReference<>((List<ProductBookRecordModel>) validateRecordCodeMessage.getExtra());
		YgUser user = SessionUtil.getCurrentUser();
		return new JSONPObject(requestParam.getCallback(), ygPrepayBookRecordService.commitOrder(Objects.requireNonNull(user).getId(), user.getUserName(),
				recordList.get(), requestParam, salesPlatform));
	}

}
